package com.qishon.apiTest;

import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.testng.Reporter;

import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @Description    封装JSoup发送网络请求代码
 * @Author         sangdi.he
 * @Time           2016-07-14 11:13
 */
public class Extract {

    /**
     * @Description    封装普通的Jsoup请求
     * @param url      url
     * @param param    参数,使用Map方式装载
     * @param meth     http请求方法
     * @return         返回一个JSONObject,请求返回结果
     * @Author         sangdi.he
     * @Time           2016-07-14 11:14
     * @Update_History 无
     */
    public static JSONObject extractJson(String url, Map<String, String> param, String meth) {

        Logger logger = Logger.getLogger("smy_1");
        String method = meth.toUpperCase();
        JSONObject bodyJson;

        //如果http方法是post
        if (method.equals("POST")) {
            Document doc = null;
            try {
                doc = Jsoup.connect(url).data(param).timeout(5000).ignoreContentType(true).post();
            } catch (IOException e) {
                e.printStackTrace();
            }
            bodyJson = new JSONObject(doc.body().html());
        }
        //如果http方法是get
        else if (method.equals("GET")) {
            Document doc = null;
            try {
                doc = Jsoup.connect(url).data(param).timeout(5000).ignoreContentType(true).get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            bodyJson = new JSONObject(doc.body().html());
        }
        else {
            bodyJson = new JSONObject();
        }

        //把param装成一个JSONObject
        JSONObject jsonParam = new JSONObject();
        for (String key : param.keySet()) {
            jsonParam.put(key, param.get(key));
        }
        //打印到控制台的日志，方便单个接口调试时用
        if (param.get("interfaceCode") != null) {
            logger.info("----->\n" + 
            		"接口:\t" + param.get("interfaceCode") + "\n" + 
            		"用例:\t" + param.get("TestCaseName") + "\n" +
            		"参数:\t" + jsonParam + "\n" +
            		"接口返回结果\t" + bodyJson.toString() + "\n" + 
            		"----------");
            Reporter.log("----->\n" + 
            		"接口:\t" + param.get("interfaceCode") + "\n" + 
            		"用例:\t" + param.get("TestCaseName") + "\n" +
            		"参数:\t" + param + "\n" +
            		"接口返回结果\t" + bodyJson.toString() + "\n" + 
            		"----------");
        }
        else {
            logger.info("----->\n" + 
            			"接口:\t" + "GetToken" + "\n" + 
            			"用例:\t" + param.get("TestCaseName") + "\n" +
            			"参数:\t" + jsonParam + "\n" +
            			"接口返回结果\t" + bodyJson.toString() + "\n" +
            			"----------");
            Reporter.log("----->\n" + 
        			"接口:\t" + "GetToken" + "\n" + 
            		"用例:\t" + param.get("TestCaseName") + "\n" +
        			"参数:\t" + param + "\n" +
        			"接口返回结果\t" + bodyJson.toString() + "\n" +
        			"----------");
        }

        return bodyJson;
    }
}
